/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import vn.webapp.dao.RoomsDAO;
import vn.webapp.dto.DataPage;
import vn.webapp.model.Classes;
import vn.webapp.model.Rooms;

@Service("roomsService")
public class RoomsServiceImpl implements RoomsService {

    @Autowired
    private RoomsDAO roomsDAO;

    /**
     *
     * @return
     */
    @Override
    public DataPage<Rooms> list() {
        DataPage<Rooms> dataRooms = new DataPage<>();
        dataRooms.setData(roomsDAO.list());
        return dataRooms;
    }

    @Override
    public Rooms viewDetail(String id) {
        return roomsDAO.viewDetail(id);
    }

    @Override
    public Rooms checkExistsName(String name, int id) {
        return roomsDAO.checkExistsName(name, id);
    }

    @Override
    public int updateTheRoom(String name, int id, int capacity) {
        return roomsDAO.updateTheRoom(name, id, capacity);
    }

    @Override
    public int removeRoom(int id) {
        return roomsDAO.removeRoom(id);
    }
    
    @Override
    public int saveARoom(String name, int capacity)
    {
        Rooms room = new Rooms();
        room.setName(name);
        room.setCapacity(capacity);
        return roomsDAO.saveARoom(room);
    }
    
    @Override
    public DataPage<Rooms> filter(int page, int pageSize) {
        DataPage<Rooms> dataRooms = new DataPage<>();
        dataRooms.setPageIndex(page);
        dataRooms.setPageSize(pageSize); 
        dataRooms.setDataCount(roomsDAO.count());
        dataRooms.setPageCount(dataRooms.getDataCount()/pageSize);
        if(dataRooms.getDataCount()%pageSize > 0){
            dataRooms.setPageCount(dataRooms.getPageCount()+1);
        }
        dataRooms.setData(roomsDAO.filter(page, pageSize));
        return dataRooms;
    }
}
